/*
* @Author: Marte
* @Date:   2018-11-01 10:02:35
* @Last Modified by:   Marte
* @Last Modified time: 2018-11-02 16:43:59
*/
function Staff(){
	this.init();
}
Staff.StaffRowTemplate=`
	<tr>
		<td class="staff-id"><%= _id %></td>
		<td class="staff-name"><%= name %></td>
		<td class="staff-dep"><%= department %></td>
		<td class="staff-pro"><%= profession %></td>
		<td class="staff-entcom"><%= entercompanytime %></td>
		<td class="staff-salary"><%= salary %></td>
		<td><a href="" class="update" data-toggle="modal" data-target="#UpdateStaffModal">修改</a> <a href="javascript:void(0);" class="del">删除</a></td>
	</tr>

`;

$.extend(Staff.prototype,{
	init(){
		this.addListener();
		this.change();
		this.loadData(1);
	},
	change(){
		$(".staff").css("background","#e6e6e6");
	},
	addListener(){
		$(".btn-add-staff").on("click", this.addStaffHandler);
		$(".pagination").on("click", "a", $.proxy(this.loadDataHandler, this));

		$("tbody").on("click",".del",$.proxy(this.delDataHandler, this));
		$("tbody").on("click",".update",$.proxy(this.updateDataHandler, this));
		$(".btn-find-user").on("click",$.proxy(this.findDataHandler, this));
	},
	loadDataHandler(event) {
		const $src = $(event.target);
		const page = Number($src.text());
		this.loadData(page);

		$src.parent("li").addClass("active").siblings("li").removeClass("active");
	},

	loadData(page) {

		page = page || 1;

		const url = "/staffs/find_by_page?page=" + page;

		$.getJSON(url, (data) => {
			if (data.res_code === 1) {
				console.log(data.res_body);
				let html = "";
				data.res_body.list.forEach((curr)=>{
					html += ejs.render(Staff.StaffRowTemplate, curr);
				});
				$(".table-staff tbody").html(html);
			}
		});
	},
	addStaffHandler(){

		const url = "/staffs/add";
		const data = $(".form-add-staff").serialize();
		$.post(url, data, (data)=>{
			if (data.res_body.status === 1) {
				console.log("成功")
//					const html = ejs.render(Staff.StaffRowTemplate, data.res_body.data)
					location.reload();
					$("#addStaffModal").modal("hide");
			} else {
				$(".add-pos-error").removeClass("hidden");
			}
		}, "json")
	},
	delDataHandler(event){
		const $src = $(event.target);
		const id=$src.parent("td").parent("tr").children(".staff-id").text();
		console.log(id);
		const url="/staffs/remove?id="+id;
		console.log(url);
		$.getJSON(url,(data)=>{
			if(data.res_code === 1){
				location.reload();
			}

		});

	},
	findDataHandler(){
		console.log(111)
	  	const name = $(".input-username").val();
	  	console.log(name)
	  	const url = "/staffs/find?name="+name;

		$.getJSON(url, (data) => {
			if (data.res_code === 1) {
				$(".table-staff tbody").html("");
				let html = "";
				data.res_body.list.forEach((curr)=>{
					html += ejs.render(Staff.StaffRowTemplate, curr);
				});
				$(".table-staff tbody").html(html);

			}
		});
	},
	updateDataHandler(event){
		const $src = $(event.target);

		const id=$src.parent("td").parent("tr").children(".staff-id").text();
//		const name=$src.parent("td").parent("tr").children(".staff-name").text();
//		const department=$src.parent("td").parent("tr").children(".staff-dep").text();
//		const profession=$src.parent("td").parent("tr").children(".staff-pro").text();
//		const entercompany=$src.parent("td").parent("tr").children(".staff-entcom").text();
//		const salary=$src.parent("td").parent("tr").children(".staff-salary").text();
		$("#updateid").val(id);

			$(".btn-update-staff").on("click",function(){

				const url = "/staffs/update";
				const data = $(".form-update-staff").serialize();


				$.post(url, data, (data)=>{
					if (data.res_body.status === 1) {
							location.reload();
							$("#UpdateStaffModal").modal("hide");
					} else {
						$(".add-pos-error").removeClass("hidden");
					}
				}, "json")
			});



	},
});
new Staff();